package com.jf.cloud.product.feign;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.product.feign.GiveawayFeignClient;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.order.vo.ShopCartItemDiscountVO;
import com.jf.cloud.common.order.vo.ShopCartItemVO;
import com.jf.cloud.common.order.vo.ShopCartVO;
import com.jf.cloud.common.product.vo.GiveawaySpuVO;
import com.jf.cloud.common.product.vo.GiveawayVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.product.service.GiveawayService;
import com.jf.cloud.product.service.GiveawaySpuService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.*;

@RestController
@Hidden
public class GiveawayFeignController implements GiveawayFeignClient {
    @Autowired
    private GiveawayService giveawayService;
    @Autowired
    private GiveawaySpuService giveawaySpuService;

    @Override
    public ServerResponseEntity<ShopCartVO> buildGiveaway(ShopCartVO shopCart) {
        if(CollectionUtils.isEmpty(shopCart.getShopCartItemDiscounts())){
            return ServerResponseEntity.success(shopCart);
        }
        for (ShopCartItemDiscountVO shopCartItemDiscount : shopCart.getShopCartItemDiscounts()) {
            List<ShopCartItemVO> shopCartItems = shopCartItemDiscount.getShopCartItems();
            buildGiveawayInfo(shopCartItems);
        }
        return ServerResponseEntity.success(shopCart);
    }

    private void buildGiveawayInfo(List<ShopCartItemVO> shopCartItems){
        Map<Long, Integer> skuMap = new HashMap<>(16);
        // 把订单中其他商品的库存减掉才能去判断赠品的库存
        for (ShopCartItemVO shopCartItem : shopCartItems) {
            Integer num = skuMap.get(shopCartItem.getSkuId());
            if (Objects.isNull(num)) {
                skuMap.put(shopCartItem.getSkuId(), 0);
                num = shopCartItem.getCount();
            } else {
                num += shopCartItem.getCount();
            }
            skuMap.put(shopCartItem.getSkuId(), num);
        }

        // 进行赠品库存判断
        for (ShopCartItemVO shopCartItem : shopCartItems) {
            GiveawayVO giveaway = giveawayService.getGiveawaySpuAndStockBySpuId(shopCartItem.getSpuId());
            if (Objects.isNull(giveaway)) {
                continue;
            }
            if (shopCartItem.getCount() >= giveaway.getBuyNum() && CollUtil.isNotEmpty(giveaway.getGiveawaySpus())) {
                // 满足赠品条件
                Integer num = shopCartItem.getCount() / giveaway.getBuyNum();
                List<GiveawaySpuVO> giveawaySpuVOList = new ArrayList<>();
                for (GiveawaySpuVO giveawaySpuVO : giveaway.getGiveawaySpus()) {
                    Long skuId = giveawaySpuVO.getSkuId();
                    Integer stock = skuMap.get(skuId);
                    if (Objects.isNull(stock)) {
                        stock = 0;
                    }
                    // 库存不足，不再显示
                    giveawaySpuVO.setSkuStock(giveawaySpuVO.getSkuStock() - stock);
                    if (giveawaySpuVO.getSkuStock() < 1) {
                        continue;
                    }

                    Integer giveawayNum = num * giveawaySpuVO.getGiveawayNum();
                    if (giveawaySpuVO.getSkuStock() < giveawayNum) {
                        giveawayNum = giveawaySpuVO.getSkuStock();
                    }
                    stock += giveawayNum;
                    skuMap.put(skuId, stock);
                    giveawaySpuVO.setSkuStock(giveawaySpuVO.getSkuStock() - giveawayNum);
                    giveawaySpuVO.setGiveawayNum(giveawayNum);
                    giveawaySpuVOList.add(giveawaySpuVO);
                }
                giveaway.setGiveawaySpuVOs(giveawaySpuVOList);
                shopCartItem.setGiveaway(giveaway);
            }
        }
    }

    @Override
    public ServerResponseEntity<Void> changeGiveawayStatus(Long spuId) {
        GiveawayVO giveawayVO = giveawayService.getBySpuId(spuId);
        if (Objects.nonNull(giveawayVO)) {
            giveawayService.changeStatus(giveawayVO.getGiveawayId(), StatusEnum.DISABLE.value());
        }
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> changeGiveawaySpuStatus(Long spuId) {
        //赠送商品则清除缓存
        List<Long> spuIds = giveawayService.listMainSpuIdByGiveawaySpuId(spuId);
        giveawaySpuService.removeGiveawayCacheBatch(spuIds);

        return ServerResponseEntity.success();
    }
}
